
//把obj对象里的值覆盖到newobj里面
function deepCopy(newobj, obj) {
  if (typeof obj != 'object') {
    return obj;
  }
  for (var attr in obj) {
    var a = {};
    if (newobj[attr]) {
      a = newobj[attr];
    }
    newobj[attr] = deepCopy(a, obj[attr]);
  }
  return newobj;
}

//当出错的时候，显示错误信息，并且跳转
function errorToBack(msg = '出错了，请重试', delta = 1) {
  wx.showToast({
    title: msg,
    icon: 'success',
    duration: 2000,
  });
  if (delta > 0) {
    setTimeout(function () {
      wx.navigateBack({
        delta: delta
      })
    }, 1000);
  }
}
//操作成功后，的提示信息
function successToShow(msg = '保存成功', callback = function () { }) {
  wx.showToast({
    title: msg,
    icon: 'success',
    duration: 2000,
  });
  setTimeout(function () {
    callback();
  }, 1500);
}


//操作失败的提示信息
function errorToShow(msg = '操作失败', callback = function () { }) {
  wx.showToast({
    title: msg,
    icon: 'none',
    duration: 1500,
  });
  setTimeout(function () {
    callback();
  }, 1500);
}

//时间戳转时间格式
function timeToDate(date) {
  var date = new Date(date * 1000);//如果date为13位不需要乘1000
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
  var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  return Y + M + D + h + m + s;
}

function time2date(micro_second) {
  var time = {};
  // 总秒数
  var second = Math.floor(micro_second);
  // 天数
  time.day = PrefixInteger(Math.floor(second / 3600 / 24), 2);
  // 小时
  time.hour = PrefixInteger(Math.floor(second / 3600 % 24), 2);
  // 分钟
  time.minute = PrefixInteger(Math.floor(second / 60 % 60), 2);
  // 秒
  time.second = PrefixInteger(Math.floor(second % 60), 2);

  var newtime = '';
  if (time.day > 0) {
    newtime = time.day + '天' + time.hour + '小时' + time.minute + '分' + time.second + '秒';
  } else {
    if (time.hour != 0) {
      newtime = time.hour + '小时' + time.minute + '分' + time.second + '秒';
    } else {
      newtime = time.minute + '分' + time.second + '秒';
    }
  }
  return newtime;
}

// 时间格式化输出，如11:03 25:19 每1s都会调用一次
 function dateformat(micro_second) {
  var time = {};
  // 总秒数
  var second = Math.floor(micro_second / 1000);
  // 天数
  time.day = PrefixInteger(Math.floor(second / 3600 / 24), 2);
  // 小时
  time.hour = PrefixInteger(Math.floor(second / 3600 % 24), 2);
  // 分钟
  time.minute = PrefixInteger(Math.floor(second / 60 % 60), 2);
  // 秒
  time.second = PrefixInteger(Math.floor(second % 60), 2);
  return time;
 }

/**
 * 使用循环的方式判断一个元素是否存在于一个数组中
 * @param {Object} arr 数组
 * @param {Object} value 元素值
 */
function isInArray(arr, value) {
  for (var i = 0; i < arr.length; i++) {
    if (value === arr[i]) {
      return true;
    }
  }
  return false;
}
module.exports = {
  deepCopy: deepCopy,
  //jumpToLogin: jumpToLogin,
  timeToDate: timeToDate,
  // formatMoney: formatMoney,
  errorToBack: errorToBack,
  successToShow: successToShow,
  // throttle: throttle,
  // groupCountDown: groupCountDown,
  // groupDetailCountDown: groupDetailCountDown,
  // seckillCountDown: seckillCountDown,
  errorToShow: errorToShow,
  time2date: time2date,
  // isPhoneNumber: isPhoneNumber,
  isInArray: isInArray
}
